<?php

/**f
 * @author christian
 * @copyright 2011
 *  
 */
 

 //establecer una variable conratafisica $_LOCALHOST
$ruta=$_SERVER['DOCUMENT_ROOT']."/viventura"."/MDTour";
include_once("$ruta/star.php");
include_once("$ruta/MDTourGeneral/CDrawPageReg.php");
include_once("$ruta/MDTourGeneral/CTourEditDraw.php");
//include_once("$rut/conf/CConfig.php");


/** Datos del paquete-tour  */
 
if (!isset($_POST['multi'])) {$_POST['multi'] = "NULL"; }

if (!isset($_GET['default'])) {$_GET['default'] = "NULL"; }
if (!isset($_GET['basic'])) {$_GET['basic'] = "NULL"; }
if (!isset($_GET['tourcreate'])) {$_GET['tourcreate'] = "NULL"; }
if (!isset($_GET['edittour'])) {$_GET['edittour'] = "NULL"; }
if (!isset($_GET['editpack'])) {$_GET['editpack'] = "NULL"; }


if (!isset($_POST['idPack'])) {$_POST['idPack'] = "NULL"; }
if (!isset($_POST['idTour'])) {$_POST['idTour'] = "NULL"; }
if (!isset($_POST['idPackTour'])) {$_POST['idPackTour'] = "NULL"; }
if (!isset($_POST['idOpe'])) {$_POST['idOpe'] = "NULL"; }



$multiple = $_POST['multi'];

$default=$_GET['default'];
$basic=$_GET['basic'];

$tourcreate=$_GET['tourcreate'];
$edittour=$_GET['edittour'];
$editpack=$_GET['editpack'];

$idPack=$_POST['idPack'];
$idTour=$_POST['idTour'];
$idPackTour=$_POST['idPackTour'];

$idOpe=$_POST['idOpe'];


/** Datos del paquete  */

if (!isset($_POST['NomPack'])) {$_POST['NomPack'] = "NULL"; }
if (!isset($_POST['CostPerOne'])) {$_POST['CostPerOne'] = "NULL"; }
if (!isset($_POST['CostPerTwo'])) {$_POST['CostPerTwo'] = "NULL"; }
if (!isset($_POST['IntComPack'])) {$_POST['IntComPack'] = "NULL"; }
if (!isset($_POST['ExtComPack'])) {$_POST['ExtComPack'] = "NULL"; }
if (!isset($_POST['NotProcPax'])) {$_POST['NotProcPax'] = "NULL"; }
if (!isset($_POST['OFICINAS_idOfi'])) {$_POST['OFICINAS_idOfi'] = "NULL"; }
if (!isset($_POST['AvaPack'])) {$_POST['AvaPack'] = "NULL"; }

$NomPack  = $_POST['NomPack'];
$CostPerOne  = $_POST['CostPerOne'];
$CostPerTwo  = $_POST['CostPerTwo'];
$IntComPack  = $_POST['IntComPack'];
$ExtComPack  = $_POST['ExtComPack'];
$NotProcPax  = $_POST['NotProcPax'];
$OFICINAS_idOfi  = $_POST['OFICINAS_idOfi'];
$AvaPack  = $_POST['AvaPack'];





/** Datos del tc */

if (!isset($_POST['idOfi'])) {$_POST['idOfi'] = "NULL"; }
if (!isset($_POST['idGuide1'])) {$_POST['idGuide1'] = "NULL"; }
if (!isset($_POST['idGuide2'])) {$_POST['idGuide2'] = "NULL"; }

$idOfi = $_POST['idOfi'];
$idGuide1 = $_POST['idGuide1'];
$idGuide2 = $_POST['idGuide2'];


/** Oficina y operador */

$query = "select OPERADOR_idOpe from oficinas where idOfi=$idOfi";
$result= CModelo::getInstance()->getData($query);
foreach($result as $r=>$c){
    $result2 = $c;
    foreach($result2 as $r2=>$c2){
        $idOpe=$c2;
    }   
}
/** Datos del tour */


    
if (!isset($_POST['from'])) {$_POST['from'] = "NULL"; }
if (!isset($_POST['to'])) {$_POST['to'] = "NULL"; }
if (!isset($_POST['PriTour'])) {$_POST['PriTour'] = "NULL"; }
if (!isset($_POST['MinPax'])) {$_POST['MinPax'] = "NULL"; }
if (!isset($_POST['MaxPax'])) {$_POST['MaxPax'] = "NULL"; }
if (!isset($_POST['OriTour'])) {$_POST['OriTour'] = "NULL"; }
if (!isset($_POST['DestTour'])) {$_POST['DestTour'] = "NULL"; }

$DateBegTour = $_POST['from'];
$uno = CDrawPageReg::getInstance()->amysql($DateBegTour);


$DateEndTour = $_POST['to'];
$dos = CDrawPageReg::getInstance()->amysql($DateEndTour);


$PriTour =$_POST['PriTour'];
$MinPax =$_POST['MinPax'];
$MaxPax=$_POST['MaxPax'];
$OriTour =$_POST['OriTour'];
$DestTour =$_POST['DestTour'];


///////////////////////////Nuevo paquete  ////////////////////////////////////////////////////////////////////////////////////////////
if($default==1){
    
    
        if($basic==1){
               $querybasic = "insert into pack (idPack,NomPack,CostPerOne,CostPerTwo,IntComPack,ExtComPack,NotProcPax,OFICINAS_idOfi,AvaPack) 
               values ('NULL', '$NomPack','$CostPerOne','$CostPerTwo','$IntComPack','$ExtComPack','$NotProcPax','$idOfi','1')";
                
               $idPack = CModelo::getInstance()->insertData($querybasic);
                //echo $idPack."/";
        }
    
        $query = "insert into tour (idTour, OriTour, DestTour, PriTour, MinPaxTour, MaxPaxTour) values ('NULL', '$OriTour', '$DestTour', '$PriTour', '$MinPax', '$MaxPax')";
        $idTourCopy = CModelo::getInstance()->insertData($query);
        //echo $query;
        //echo $idTourCopy."/";
            
        $query = "insert into pack_has_tour (idPackTour,PACK_idPack,TOUR_idTour,DateBegTour,DateEndTour,DefaultTourPack) values ('NULL', '$idPack','$idTourCopy','$uno','$dos',1)";

        $idPackTour = CModelo::getInstance()->insertData($query);
    //    echo $idPackTour."/";
    
        $query = "insert into pack_has_tour_has_guidetc (idPackTourGui,guidetc_idGuide,pack_has_tour_idPackTour) values ('NULL','$idGuide1','$idPackTour'),('NULL','$idGuide2','$idPackTour')";
        $idPackTourGui = CModelo::getInstance()->insertData($query);
        //echo $idPackTourGui."/";
               
        //registrando extensiones
        if($multiple!="NULL"){
        foreach($multiple as $r=>$c){ 
            $query = "insert into pack_has_tour_has_extensiones (idExtTour,pack_has_tour_idPackTour,extensiones_idExt,operador_idOpe) values ('NULL','$idPackTour','$c','$idOpe')";
        //    echo $query;
            $extension = CModelo::getInstance()->insertData($query);
        }}
                
        //echo "<script> parent.location.href = '/viventura/MDTour/index.php?updated=$idPack' </script>"; 
            }

if($tourcreate==1){
    //////// Personalizar el nombre del tour, se pasa del NamTypTour (default) a campo personalizado en pack_has_tour//////////
      if (!isset($_POST['NomPack'])) {$_POST['NomPack'] = "NULL"; }
      $NamTour  = $_POST['NomPack'];
      
      
     //echo $idPackTour;  
     $query = "insert into pack_has_tour (idPackTour,NamTour,PACK_idPack,TOUR_idTour,DateBegTour,DateEndTour,DefaultTourPack) values ('NULL','$NamTour', '$idPack','$idTour','$uno','$dos',0)";
     //echo $query;     
     $idPackTourNew = CModelo::getInstance()->insertData($query);
     

     //echo $idPackTourNew;
     copying_tracks($idPackTour,$idPackTourNew);
     copying_itinerary_rest($idPackTour,$idPackTourNew);
     copying_itinerary_trans($idPackTour,$idPackTourNew);
     copying_itinerary_guide($idPackTour,$idPackTourNew);
     copying_itinerary_ops($idPackTour,$idPackTourNew);      
    //echo $query;
    
 //echo "<script> parent.location.href = '../MDItinerary/MDEItinerary.php?codigo=$idPackTourNew&default=0</script>";
    //echo "sdgsdgsd";
     if($multiple=="NULL"){
        $query = "delete from pack_has_tour_has_extensiones where pack_has_tour_idPackTour=$idPackTourNew";
        CModelo::getInstance()->insertData($query);
 
        }
     else{
        $query = "delete from pack_has_tour_has_extensiones where pack_has_tour_idPackTour=$idPackTourNew";
        CModelo::getInstance()->insertData($query);
        //echo $query;
           
     
     $idOfi = CTourEditDraw::getInstance()->getAnyId("pack","idPack",$idPack,"OFICINAS_idOfi");
     $idOpe = CTourEditDraw::getInstance()->getAnyId("oficinas","idOfi",$idOfi,"OPERADOR_idOpe");   
      
      
      $query = "insert into pack_has_tour_has_guidetc (idPackTourGui,guidetc_idGuide,pack_has_tour_idPackTour,NumGui) values ('NULL','$idGuide1','$idPackTourNew','1'),('NULL','$idGuide2','$idPackTourNew','2')";
      echo $query;
      $idPackTourGui = CModelo::getInstance()->insertData($query);
        
              
        foreach($multiple as $r=>$c){ 
        
        $query = "insert into pack_has_tour_has_extensiones (idExtTour,pack_has_tour_idPackTour,extensiones_idExt,operador_idOpe) values ('NULL','$idPackTourNew','$c','$idOpe')";
        //echo $query;
        $extension = CModelo::getInstance()->insertData($query);
        }
        

       
        
        //echo "<script> parent.location.href = '/viventura/MDTour/index.php?updated=$idPack' </script>";
       
    }
    // echo "<script> parent.location.href = '../MDItinerary/MDEItinerary.php?codigo=$idPackTourNew&default=0</script>";
    
    echo "<script> parent.location.href = '../MDItinerary/MDEItinerary.php?codigo=$idPackTourNew&default=0' </script>";
 
  
}
        
 
if($edittour==1){
    
    if (!isset($_POST['NamTour'])) {$_POST['NamTour'] = "NULL"; }
    $NamTour  = $_POST['NamTour'];
      
    //echo $idPackTour;  
    //(idPackTour,NamTour,PACK_idPack,TOUR_idTour,DateBegTour,DateEndTour,DefaultTourPack)
    $sql = "UPDATE  `viv`.`pack_has_tour` SET  
        `NamTour` =  '$NamTour',
        `DateBegTour` =  '$uno',
        `DateEndTour` =  '$dos' WHERE  `pack_has_tour`.`idPackTour` = $idPackTour";
        //echo $sql;
        
        //echo $idGuide1;echo $idGuide2;
    CModelo::getInstance()->insertData($sql);
    
}


if($editpack==1){
    $sql = "UPDATE  `viv`.`pack` SET  
    `NomPack` =  '$NomPack',
    `CostPerOne` =  '$CostPerOne',
    `CostPerTwo` =  '$CostPerTwo',
    `IntComPack` = '$IntComPack' WHERE  `pack`.`idPack`= $idPack";
    //echo $sql;
    CModelo::getInstance()->insertData($sql);
    
}





function copying_tracks($oldTourPack,$newTourPack){

    $tracks = 0; //Inicialmente se supone que existen 0 tracks en el paquete base
    
    
    $query = "select OrdTrack from tour_has_track where TOUR_idTour='$oldTourPack' ORDER BY OrdTrack DESC LIMIT 0, 1";
    $duration = CModelo::getInstance()->getData($query);
    
    //echo $query;
    foreach($duration as $r => $c){
        $table1 = $c;
        foreach($table1 as $r1 => $c1){
                $tracks = $c1;    
        }            
    }
    //echo $tracks;
    $o=1;
    if($tracks>0){
        
        while($o<=$tracks){
        $query = "select idTourTrack,TOUR_idTour,TRACK_idTrack,RESTAURANT_idRest,OrdTrack,DateBegTrack,DateEndTrack 
                from tour_has_track where Tour_idTour = $oldTourPack and OrdTrack=$o";            
        //echo $query;
        $result = CModelo::getInstance()->getData($query);
    
           foreach($result as $r=>$c){
            $table1=$c;
                foreach($table1 as $r2=>$c2){
                    $table2[] = $c2;
                }
            }
            //echo $table2[6];
        
       // var_dump($table2);
        
        $insert = "insert into tour_has_track (idTourTrack,TOUR_idTour,TRACK_idTrack,RESTAURANT_idRest,OrdTrack,DateBegTrack,DateEndTrack) 
                   values (NULL,'$newTourPack','$table2[2]','$table2[3]','$table2[4]','$table2[5]','$table2[6]')";
        $copyTrack = CModelo::getInstance()->insertData($insert);
      //  echo $insert;
//        echo $copyTrack;    
        $o++;
        $table2="";
        }
    }
    else{ //actualizacion del paquete
        
        
        
     }
     //var_dump($table1);
    
    
}


function copying_itinerary_rest($oldTourPack,$newTourPack){
    
//	ResPackTour			pack_has_tour_idPackTour 	SerFlag	DayActRes	NumRecRes		CanIteRec	PriUni	MonPag	MonSol	MonCha	AceRec	MonTot	CodIte	CommSer

    $count = "SELECT count(`restaurant_has_pack_has_tour`.`pack_has_tour_idPackTour`) FROM restaurant_has_pack_has_tour
    WHERE(( pack_has_tour_idPackTour = $oldTourPack))"; 
    //echo $count;
    $anscount = CModelo::getInstance()->getData($count);
    foreach ($anscount as $r1 => $k1){ $anscount2 = $k1; foreach ($anscount2 as $m1 => $n1 ){ $cuenta_total = $n1;}}
    //echo $cuenta_total;
    
    
    $i=1;
    while($i<=$cuenta_total){      
    $source = "select restaurant_idRest, DesSer, DayActRes, DateActRes, CanIteRec, PriUni, CommSer from restaurant_has_pack_has_tour where pack_has_tour_idPackTour = $oldTourPack 
              ORDER BY ResPackTour DESC LIMIT $i, 1";
    //echo $source;
    $ans = CModelo::getInstance()->getData($source);
    foreach ($ans as $r => $k){ $ans2 = $k; foreach ($ans2 as $r1 => $k1 ){ $servicio[] = $k1;}}
    
    //var_dump($servicio);   
    $source_copy = "insert into restaurant_has_pack_has_tour (restaurant_idRest, DesSer, DayActRes, DateActRes, CanIteRec, PriUni, CommSer, pack_has_tour_idPackTour) 
                    values ('$servicio[0]','$servicio[1]','$servicio[2]','$servicio[3]','$servicio[4]','$servicio[5]','$servicio[6]','$newTourPack')";
    //echo $source_copy;
    $copiados = CModelo::getInstance()->insertData($source_copy);
    //echo $copiados. "R/";
    $i = $i+1;
       
        
   }
    
}    
    
    function copying_itinerary_trans($oldTourPack,$newTourPack){
    
//	ResPackTour			pack_has_tour_idPackTour 	SerFlag	DayActRes	NumRecRes		CanIteRec	PriUni	MonPag	MonSol	MonCha	AceRec	MonTot	CodIte	CommSer

    $count = "SELECT count(`transporte_has_pack_has_tour`.`pack_has_tour_idPackTour`) FROM transporte_has_pack_has_tour
    WHERE(( pack_has_tour_idPackTour = $oldTourPack))"; 
    //echo $count;
    $anscount = CModelo::getInstance()->getData($count);
    foreach ($anscount as $r1 => $k1){ $anscount2 = $k1; foreach ($anscount2 as $m1 => $n1 ){ $cuenta_total = $n1;}}
    //echo $cuenta_total;
    
    
    $i=1;
    while($i<=$cuenta_total){
              
    $source = "select transporte_idTrans, DesSer, DayActTra, DateActTra, CanIteRec, PriUni, CommSer from transporte_has_pack_has_tour where pack_has_tour_idPackTour = $oldTourPack 
              ORDER BY TranPackTour DESC LIMIT $i, 1";
    //echo $source;
    $ans = CModelo::getInstance()->getData($source);
    foreach ($ans as $r => $k){ $ans2 = $k; foreach ($ans2 as $r1 => $k1 ){ $servicio[] = $k1;}}
    
    //var_dump($servicio);   
    $source_copy = "insert into transporte_has_pack_has_tour (transporte_idTrans, DesSer, DayActTra, DateActTra, CanIteRec, PriUni, CommSer, pack_has_tour_idPackTour) 
                    values ('$servicio[0]','$servicio[1]','$servicio[2]','$servicio[3]','$servicio[4]','$servicio[5]','$servicio[6]','$newTourPack')";
    //echo $source_copy;
    $copiados = CModelo::getInstance()->insertData($source_copy);
    //echo $copiados."/T";
    $i = $i+1;
   
   }
   
   
   function copying_itinerary_guide($oldTourPack,$newTourPack){
    
//	ResPackTour			pack_has_tour_idPackTour 	SerFlag	DayActRes	NumRecRes		CanIteRec	PriUni	MonPag	MonSol	MonCha	AceRec	MonTot	CodIte	CommSer

    $count = "SELECT count(`pack_has_tour_has_guidetc`.`pack_has_tour_idPackTour`) FROM pack_has_tour_has_guidetc
    WHERE(( pack_has_tour_idPackTour = $oldTourPack))"; 
    //echo $count;
    $anscount = CModelo::getInstance()->getData($count);
    foreach ($anscount as $r1 => $k1){ $anscount2 = $k1; foreach ($anscount2 as $m1 => $n1 ){ $cuenta_total = $n1;}}
    //echo $cuenta_total;
    
    
    $i=1;
    while($i<=$cuenta_total){
              
    $source = "select guidetc_idGuide, DesSer, DayActTra, DateActTra, CanIteRec, PriUni, CommSer from pack_has_tour_has_guidetc where pack_has_tour_idPackTour = $oldTourPack 
              ORDER BY idPackTourGui DESC LIMIT $i, 1";
    //echo $source;
    $ans = CModelo::getInstance()->getData($source);
    foreach ($ans as $r => $k){ $ans2 = $k; foreach ($ans2 as $r1 => $k1 ){ $servicio[] = $k1;}}
    
    //var_dump($servicio);   
    $source_copy = "insert into pack_has_tour_has_guidetc (guidetc_idGuide, DesSer, DayActTra, DateActTra, CanIteRec, PriUni, CommSer, pack_has_tour_idPackTour) 
                    values ('$servicio[0]','$servicio[1]','$servicio[2]','$servicio[3]','$servicio[4]','$servicio[5]','$servicio[6]','$newTourPack')";
    //echo $source_copy;
    $copiados = CModelo::getInstance()->insertData($source_copy);
    //echo $copiados."/G";
    $i = $i+1;
   
   }
   
}


   function copying_itinerary_ops($oldTourPack,$newTourPack){
    
//	ResPackTour			pack_has_tour_idPackTour 	SerFlag	DayActRes	NumRecRes		CanIteRec	PriUni	MonPag	MonSol	MonCha	AceRec	MonTot	CodIte	CommSer

    $count = "SELECT count(`actividades_has_tour_has_pasajero`.`pack_has_tour_idPackTour`) FROM actividades_has_tour_has_pasajero
    WHERE(( pack_has_tour_idPackTour = $oldTourPack))"; 
    //echo $count;
    $anscount = CModelo::getInstance()->getData($count);
    foreach ($anscount as $r1 => $k1){ $anscount2 = $k1; foreach ($anscount2 as $m1 => $n1 ){ $cuenta_total = $n1;}}
    //echo $cuenta_total;
    
    
    $i=1;
    while($i<=$cuenta_total){
              
    $source = "select operador_idOpe, DesSer, DayActTra, DateActTra, CanIteRec, PriUni, CommSer, actividades_idAct from actividades_has_tour_has_pasajero where pack_has_tour_idPackTour = $oldTourPack 
              ORDER BY idActPaxTour DESC LIMIT $i, 1";
    //echo $source;
    $ans = CModelo::getInstance()->getData($source);
    foreach ($ans as $r => $k){ $ans2 = $k; foreach ($ans2 as $r1 => $k1 ){ $servicio[] = $k1;}}
    
    //var_dump($servicio);   
    $source_copy = "insert into actividades_has_tour_has_pasajero (operador_idOpe, DesSer, DayActTra, DateActTra, CanIteRec, PriUni, CommSer, actividades_idAct, pack_has_tour_idPackTour) 
                    values ('$servicio[0]','$servicio[1]','$servicio[2]','$servicio[3]','$servicio[4]','$servicio[5]','$servicio[6]','$servicio[7]','$newTourPack')";
    //echo $source_copy;
    $copiados = CModelo::getInstance()->insertData($source_copy);
    //echo $copiados."/O";
    $i = $i+1;
   
   }
   
}  


   
//    var_dump($servicio);
        
}


?>